﻿using Abp.Application.Services.Dto;
using Abp.Application.Services;
using Abp.Domain.Repositories;
//using Choucius.Boilerplate9.UR5002;
using System.Threading.Tasks;
using System;
using System.Text;
using Choucius.Boilerplate9.PlasticContainerLogs.Dto;

namespace Choucius.Boilerplate9.PlasticContainerLogs
{
    public class PlasticContainerLogAppService : AsyncCrudAppService<PlasticContainerLog, PlasticContainerLogDto, long, PagedResultRequestDto, CreatePlasticContainerLogDto, CreatePlasticContainerLogDto>, IPlasticContainerLogAppService
    {
        private readonly IRepository<PlasticContainerLog, long> _repository;

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="repository"></param>
        public PlasticContainerLogAppService(IRepository<PlasticContainerLog, long> repository) : base(repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 调用UR5002读写器读取一个RFID标签，并保存标签信息
        /// </summary>
        /// <returns></returns>
        public async Task UR5002ReadAndSaveAsync()
        {
            //// 连接UR5002
            //int port = 0;       // 端口：COM4
            //byte comAddr = 255; // COM地址
            //byte baud = 5;      // 波特率：
            //int portHandle = 0; // 
            //int openReturn = UR5002Basic.AutoOpenComPort(ref port, ref comAddr, baud, ref portHandle);
            //if (openReturn != 0)
            //{
            //    throw new Exception("串口通讯错误");
            //}
            ////OpenUR5002Port(port);

            //// 从UR5002读取RFID标签EPC
            //byte[] EPC = new byte[5000];
            //int Totallen = 0;
            //int CardNum = 0;
            //var inventoryReturn = UR5002Basic.Inventory_G2(ref comAddr, 0, 0, 0, EPC, ref Totallen, ref CardNum, portHandle);
            //if (inventoryReturn != 1 && inventoryReturn != 2 && inventoryReturn != 3 && inventoryReturn != 4 && inventoryReturn != 0xFB)
            //{
            //    throw new Exception("读码错误");
            //}
            //byte[] daw = new byte[Totallen];
            //Array.Copy(EPC, daw, Totallen);
            //var temps = ByteArrayToHexString(daw);
            //var EPClen = daw[0];
            //var sEPC = temps.Substring(0 * 2 + 2, EPClen * 2);

            //// 关闭UR5002的连接
            //int closeReturn = UR5002Basic.CloseSpecComPort(port);
            //if (closeReturn != 0)
            //{
            //    throw new Exception("串口关闭错误");
            //}

            //// 保存
            //var entity = new PlasticContainerLog();
            //entity.RFID = sEPC;
            //entity.Status = Enum.LabelStatus.Undefined;
            //entity.CreationTime = DateTime.Now;
            //entity.CreatorUserId = 0;

            //await _repository.InsertAsync(entity); // 使用InsertAsync方法保存实体
        }

        private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
            return sb.ToString().ToUpper();
        }
    }
}
